home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2001 May / SGI IRIX Base Documentation 2001 May.iso / usr / share / catman / p_man / cat3g / regcomp.z / regcomp
Encoding:
Text File  |  1998-10-20  |  21.9 KB  |  397 lines

  1.  
  2.  
  3.  
  4. rrrreeeeggggccccoooommmmpppp((((3333GGGG))))                                                        rrrreeeeggggccccoooommmmpppp((((3333GGGG))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      _rrrr_eeee_gggg_cccc_oooo_mmmm_pppp: _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc, _rrrr_eeee_gggg_eeee_rrrr_rrrr_oooo_rrrr, _rrrr_eeee_gggg_ffff_rrrr_eeee_eeee - regular expression matching
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.      _####_iiii_nnnn_cccc_llll_uuuu_dddd_eeee _<<<<_ssss_yyyy_ssss_////_tttt_yyyy_pppp_eeee_ssss_...._hhhh_>>>>
  13.  
  14.      _####_iiii_nnnn_cccc_llll_uuuu_dddd_eeee _<<<<_rrrr_eeee_gggg_eeee_xxxx_...._hhhh_>>>>
  15.  
  16.      _iiii_nnnn_tttt _rrrr_eeee_gggg_cccc_oooo_mmmm_pppp _((((_rrrr_eeee_gggg_eeee_xxxx______tttt _****_p_r_e_g_,,,, _cccc_oooo_nnnn_ssss_tttt _cccc_hhhh_aaaa_rrrr _****_p_a_t_t_e_r_n_,,,, _iiii_nnnn_tttt _c_f_l_a_g_s_))))_;;;;
  17.  
  18.      _iiii_nnnn_tttt  _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc _((((_cccc_oooo_nnnn_ssss_tttt _rrrr_eeee_gggg_eeee_xxxx______tttt _****_p_r_e_g_,,,, _cccc_oooo_nnnn_ssss_tttt _cccc_hhhh_aaaa_rrrr _****_s_t_r_i_n_g_,,,, _ssss_iiii_zzzz_eeee______tttt _n_m_a_t_c_h_,,,,
  19.      _rrrr_eeee_gggg_mmmm_aaaa_tttt_cccc_hhhh______tttt _p_m_a_t_c_h[]_,,,, _iiii_nnnn_tttt _e_f_l_a_g_s_))))_;;;;
  20.  
  21.      _ssss_iiii_zzzz_eeee______tttt _rrrr_eeee_gggg_eeee_rrrr_rrrr_oooo_rrrr _((((_iiii_nnnn_tttt  _e_r_r_c_o_d_e_,,,, _cccc_oooo_nnnn_ssss_tttt _rrrr_eeee_gggg_eeee_xxxx______tttt _****_p_r_e_g_,,,, _cccc_hhhh_aaaa_rrrr _****_e_r_r_b_u_f_,,,, _ssss_iiii_zzzz_eeee______tttt
  22.      _e_r_r_b_u_f__s_i_z_e_))))_;;;;
  23.  
  24.      _ssss_iiii_zzzz_eeee______tttt _rrrr_eeee_gggg_ffff_rrrr_eeee_eeee _((((_rrrr_eeee_gggg_eeee_xxxx______tttt _****_p_r_e_g_))))_;;;;
  25.  
  26. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  27.      The structure type regex_t contains the following members:
  28.  
  29.            MEMBER                 MEANING
  30.            _____________________________________________________________
  31.            int re_magic           RE magic number
  32.            size_t re_nsub         number of parenthesized subexpressions
  33.            const char *re_endp    end pointer for REG_PEND
  34.            struct re_guts *re_g   internal RE data structure
  35.  
  36.      The structure type regmatch_t contains the following members:
  37.  
  38.             MEMBER           MEANING
  39.             ___________________________________________________________
  40.             regoff_t rm_so   Byte offset from start of string to start
  41.                              of substring
  42.             regoff_t rm_eo   Byte offset from start of string of the
  43.                              first character after the end of substring
  44.  
  45.      The _rrrr_eeee_gggg_cccc_oooo_mmmm_pppp() function will compile the regular expression contained in
  46.      the string pointed to by the _p_a_t_t_e_r_n argument and place the results in
  47.      the structure pointed to by _p_r_e_g.  The _c_f_l_a_g_s argument is the bitwise
  48.      inclusive OR of zero or more of the following flags, which are defined in
  49.      the header _<<<<_rrrr_eeee_gggg_eeee_xxxx_...._hhhh_>>>>:
  50.  
  51.              _________________________________________________________
  52.              REG_EXTENDED   Use Extended Regular Expressions.
  53.              REG_ICASE      Ignore case in match.
  54.              REG_NOSUB      Report only success/fail in regexec() .
  55.              REG_NEWLINE    Change the handling of newline characters,
  56.                             as described in the text.
  57.  
  58.      The default regular expression type for _p_a_t_t_e_r_n is a _BBBB_aaaa_ssss_iiii_cccc _RRRR_eeee_gggg_uuuu_llll_aaaa_rrrr
  59.      _EEEE_xxxx_pppp_rrrr_eeee_ssss_ssss_iiii_oooo_nnnn. The application can specify _EEEE_xxxx_tttt_eeee_nnnn_dddd_eeee_dddd _RRRR_eeee_gggg_uuuu_llll_aaaa_rrrr _EEEE_xxxx_pppp_rrrr_eeee_ssss_ssss_iiii_oooo_nnnn_ssss
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. rrrreeeeggggccccoooommmmpppp((((3333GGGG))))                                                        rrrreeeeggggccccoooommmmpppp((((3333GGGG))))
  71.  
  72.  
  73.  
  74.      using the REG_EXTENDED _c_f_l_a_g_s flag.
  75.  
  76.      On successful completion, it returns 0; otherwise it returns non-zero,
  77.      and the content of _p_r_e_g is undefined.
  78.  
  79.      If the REG_NOSUB flag was not set in _c_f_l_a_g_s, then _rrrr_eeee_gggg_cccc_oooo_mmmm_pppp()  will set
  80.      _r_e__n_s_u_b to the number of parenthesised subexpressions (delimited by \( \)
  81.      in _bbbb_aaaa_ssss_iiii_cccc _rrrr_eeee_gggg_uuuu_llll_aaaa_rrrr _eeee_xxxx_pppp_rrrr_eeee_ssss_ssss_iiii_oooo_nnnn_ssss or ( ) in _eeee_xxxx_tttt_eeee_nnnn_dddd_eeee_dddd _rrrr_eeee_gggg_uuuu_llll_aaaa_rrrr _eeee_xxxx_pppp_rrrr_eeee_ssss_ssss_iiii_oooo_nnnn_ssss)
  82.      found in _p_a_t_t_e_r_n.
  83.  
  84.      The _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  function compares the null-terminated string specified by
  85.      _s_t_r_i_n_g with the compiled regular expression _p_r_e_g initialised by a
  86.      previous call to _rrrr_eeee_gggg_cccc_oooo_mmmm_pppp().  If it finds a match, _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  returns 0;
  87.      otherwise it returns non-zero indicating either no match or an error.
  88.      The _e_f_l_a_g_s argument is the bitwise inclusive OR of zero or more of the
  89.      following flags, which are defined in the header _<<<<_rrrr_eeee_gggg_eeee_xxxx_...._hhhh_>>>>:
  90.  
  91.             __________________________________________________________
  92.             REG_NOTBOL   The first character of the string pointed to
  93.                          by _s_t_r_i_n_g is not the beginning of the line.
  94.                          Therefore, the circumflex character (^), when
  95.                          taken as a special character, will not match
  96.                          the beginning of _s_t_r_i_n_g.
  97.  
  98.             REG_NOTEOL   The last character of the string pointed to
  99.                          by _s_t_r_i_n_g is not the end of the line.
  100.                          Therefore, the dollar sign ($), when taken
  101.                          as a special character, will not match the
  102.                          end of _s_t_r_i_n_g.
  103.  
  104.      If _n_m_a_t_c_h is 0 or REG_NOSUB was set in the _c_f_l_a_g_s argument to _rrrr_eeee_gggg_cccc_oooo_mmmm_pppp() ,
  105.      then _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  will ignore the _p_m_a_t_c_h argument. Otherwise, the _p_m_a_t_c_h
  106.      argument must point to an array with at least _n_m_a_t_c_h elements, and
  107.      _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  will fill in the elements of that array with offsets of the
  108.      substrings of _s_t_r_i_n_g that correspond to the parenthesised subexpressions
  109.      of _p_a_t_t_e_r_n: _p_m_a_t_c_h[_i]._r_m__s_o will be the byte offset of the beginning and
  110.      _p_m_a_t_c_h[_i]._r_m__e_o will be one greater than the byte offset of the end of
  111.      substring i. (Subexpression i begins at the ith matched open parenthesis,
  112.      counting from 1.) Offsets in _p_m_a_t_c_h[_0] identify the substring that
  113.      corresponds to the entire regular expression. Unused elements of _p_m_a_t_c_h
  114.      up to _p_m_a_t_c_h[_n_m_a_t_c_h-_1] will be filled with -1. If there are more than
  115.      _n_m_a_t_c_h subexpressions in _p_a_t_t_e_r_n (_p_a_t_t_e_r_n itself counts as a
  116.      subexpression), then _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  will still do the match, but will record
  117.      only the first _n_m_a_t_c_h substrings.
  118.  
  119.      When matching a _bbbb_aaaa_ssss_iiii_cccc or _eeee_xxxx_tttt_eeee_nnnn_dddd_eeee_dddd _rrrr_eeee_gggg_uuuu_llll_aaaa_rrrr _eeee_xxxx_pppp_rrrr_eeee_ssss_ssss_iiii_oooo_nnnn, any given
  120.      parenthesised subexpression of _p_a_t_t_e_r_n might participate in the match of
  121.      several different substrings of _s_t_r_i_n_g, or it might not match any
  122.      substring even though the pattern as a whole did match. The following
  123.      rules are used to determine which substrings to report in _p_m_a_t_c_h when
  124.      matching regular expressions:
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. rrrreeeeggggccccoooommmmpppp((((3333GGGG))))                                                        rrrreeeeggggccccoooommmmpppp((((3333GGGG))))
  137.  
  138.  
  139.  
  140.           1. If subexpression i in a regular expression is not
  141.                contained within another subexpression, and it participated in
  142.                the match several times, then the byte offsets in _p_m_a_t_c_h[_i]
  143.                will delimit the last such match.
  144.  
  145.  
  146.           2. If subexpression i is not contained within another
  147.                subexpression, and it did not participate in an otherwise"
  148.                successful match, the byte offsets in _p_m_a_t_c_h[_i] will be -1.  A
  149.                subexpression does not participate in the match when: * or \{
  150.                \} appears immediately after the subexpression in a basic
  151.                regular expression, or *, ?, or { } appears immediately after
  152.                the subexpression in an extended regular expression, and the
  153.                subexpression did not match (matched 0 times)
  154.  
  155.                or:
  156.  
  157.                | is used in an extended regular expression to select this
  158.                subexpression or another, and the other subexpression matched.
  159.  
  160.  
  161.           3.If subexpression i is contained within another
  162.                subexpression j, and i is not contained within any other
  163.                subexpression that is contained within j, and a match of
  164.                subexpression j is reported in _p_m_a_t_c_h[_j], then the match or
  165.                non-match of subexpression i reported in _p_m_a_t_c_h[_i] will be as
  166.                described in 1. and 2. above, but within the substring reported
  167.                in _p_m_a_t_c_h[_j] rather than the whole string.
  168.  
  169.  
  170.           4.If subexpression i is contained in subexpression j, and
  171.                the byte offsets in _p_m_a_t_c_h[_j] are -1, then the pointers in
  172.                _p_m_a_t_c_h[_i] also will be -1.
  173.  
  174.  
  175.           5.If subexpression i matched a zero-length string, then
  176.                both byte offsets in _p_m_a_t_c_h[_i] will be the byte offset of the
  177.                character or null terminator immediately following the zero-
  178.                length string.
  179.  
  180.      If, when _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  is called, the locale is different from when the
  181.      regular expression was compiled, the result is undefined.
  182.  
  183.      If REG_NEWLINE is not set in _c_f_l_a_g_s, then a newline character in _p_a_t_t_e_r_n
  184.      or _s_t_r_i_n_g will be treated as an ordinary character. If REG_NEWLINE is
  185.      set, then newline will be treated as an ordinary character except as
  186.      follows:
  187.  
  188.  
  189.           1.A newline character in _s_t_r_i_n_g will not be matched by a
  190.                period outside a bracket expression or by any form of a non-
  191.                matching list
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. rrrreeeeggggccccoooommmmpppp((((3333GGGG))))                                                        rrrreeeeggggccccoooommmmpppp((((3333GGGG))))
  203.  
  204.  
  205.  
  206.           2.A circumflex (^) in _p_a_t_t_e_r_n, when used to specify
  207.                expression anchoring, will match the zero-length string
  208.                immediately after a newline in _s_t_r_i_n_g, regardless of the
  209.                setting of REG_NOTBOL.
  210.  
  211.  
  212.           3.A dollar-sign ($) in _p_a_t_t_e_r_n, when used to specify
  213.                expression anchoring, will match the zero-length string
  214.                immediately before a newline in _s_t_r_i_n_g, regardless of the
  215.                setting of REG_NOTEOL.
  216.  
  217.      The _rrrr_eeee_gggg_ffff_rrrr_eeee_eeee()  function frees any memory allocated by _rrrr_eeee_gggg_cccc_oooo_mmmm_pppp()
  218.      associated with _p_r_e_g.
  219.  
  220.      The following constants are defined as error return values:
  221.  
  222.            _____________________________________________________________
  223.            REG_NOMATCH    _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  failed to match.
  224.  
  225.            REG_BADPAT     Invalid regular expression.
  226.  
  227.            REG_ECOLLATE   Invalid collating element referenced.
  228.  
  229.            REG_ECTYPE     Invalid character class type referenced.
  230.  
  231.            REG_EESCAPE    Trailing \ in pattern.
  232.  
  233.            REG_ESUBREG    Number in \digit invalid or in error.
  234.  
  235.            REG_EBRACK     [ ] imbalance.
  236.  
  237.            REG_ENOSYS     The function is not supported.
  238.  
  239.            REG_EPAREN     \( \) or ( ) imbalance.
  240.  
  241.            REG_EBRACE     \{ \} imbalance.
  242.  
  243.            REG_BADBR      Content of \{ \} invalid: not a number, number
  244.                           too large, more than two numbers, first
  245.                           larger than second.
  246.  
  247.            REG_ERANGE     Invalid endpoint in range expression.
  248.  
  249.            REG_ESPACE     Out of memory.
  250.  
  251.            REG_BADRPT     ?, * or + not preceded by valid regular
  252.                           expression.
  253.  
  254.      The _rrrr_eeee_gggg_eeee_rrrr_rrrr_oooo_rrrr()  function provides a mapping from error codes returned by
  255.      _rrrr_eeee_gggg_cccc_oooo_mmmm_pppp()  and _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  to unspecified printable strings. It generates
  256.      a string corresponding to the value of the _e_r_r_c_o_d_e argument, which must
  257.      be the last non-zero value returned by _rrrr_eeee_gggg_cccc_oooo_mmmm_pppp()  or _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  with the
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. rrrreeeeggggccccoooommmmpppp((((3333GGGG))))                                                        rrrreeeeggggccccoooommmmpppp((((3333GGGG))))
  269.  
  270.  
  271.  
  272.      given value of _p_r_e_g.  If _e_r_r_c_o_d_e is not such a value, the content of the
  273.      generated string is unspecified.
  274.  
  275.      If _p_r_e_g is a null pointer, but _e_r_r_c_o_d_e is a value returned by a previous
  276.      call to _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  or _rrrr_eeee_gggg_cccc_oooo_mmmm_pppp(), the _rrrr_eeee_gggg_eeee_rrrr_rrrr_oooo_rrrr()  still generates an error
  277.      string corresponding to the value of _e_r_r_c_o_d_e.
  278.  
  279.      If the _e_r_r_b_u_f__s_i_z_e argument is not 0, _rrrr_eeee_gggg_eeee_rrrr_rrrr_oooo_rrrr() will place the generated
  280.      string into the buffer of size _e_r_r_b_u_f__s_i_z_e bytes pointed to by _e_r_r_b_u_f. If
  281.      the string (including the terminating null) cannot fit in the buffer,
  282.      _rrrr_eeee_gggg_eeee_rrrr_rrrr_oooo_rrrr()  will truncate the string and null-terminate the result.
  283.  
  284.      If _e_r_r_b_u_f__s_i_z_e is 0, _rrrr_eeee_gggg_eeee_rrrr_rrrr_oooo_rrrr()  ignores the _e_r_r_b_u_f argument, and returns
  285.      the size of the buffer needed to hold the generated string.
  286.  
  287.      If the _p_r_e_g argument to _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  or _rrrr_eeee_gggg_ffff_rrrr_eeee_eeee()  is not a compiled
  288.      regular expression returned by _rrrr_eeee_gggg_cccc_oooo_mmmm_pppp() , the result is undefined. A
  289.      _p_r_e_g is no longer treated as a compiled regular expression after it is
  290.      given to _rrrr_eeee_gggg_ffff_rrrr_eeee_eeee() .
  291.  
  292.  
  293. RRRREEEETTTTUUUURRRRNNNN VVVVAAAALLLLUUUUEEEE
  294.      On successful completion, the _rrrr_eeee_gggg_cccc_oooo_mmmm_pppp()  function returns 0.  Otherwise,
  295.      it returns an integer value indicating an error as described in
  296.      _<<<<_rrrr_eeee_gggg_eeee_xxxx_...._hhhh_>>>>, and the content of _p_r_e_g is undefined.
  297.  
  298.      On successful completion, the _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  function returns 0.  Otherwise
  299.      it returns REG_NOMATCH to indicate no match, or REG_ENOSYS to indicate
  300.      that the function is not supported.
  301.  
  302.      Upon successful completion, the _rrrr_eeee_gggg_eeee_rrrr_rrrr_oooo_rrrr()  function returns the number
  303.      of bytes needed to hold the entire generated string. Otherwise, it
  304.      returns 0 to indicate that the function is not implemented.
  305.  
  306.      The _rrrr_eeee_gggg_ffff_rrrr_eeee_eeee()  function returns no value.
  307.  
  308.  
  309. EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  310.      #include _<<<<_rrrr_eeee_gggg_eeee_xxxx_...._hhhh_>>>>
  311.  
  312.      /*
  313.       * Match string against the extended regular expression in
  314.       * pattern, treating errors as no match.
  315.       *
  316.       * return 1 for match, 0 for no match
  317.       */
  318.  
  319.      int match(const char *string, char *pattern) {
  320.        int status;
  321.        regex_t re;
  322.  
  323.        if (regcomp(&re, pattern, REG_EXTENDED | REG_NOSUB) != 0)
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. rrrreeeeggggccccoooommmmpppp((((3333GGGG))))                                                        rrrreeeeggggccccoooommmmpppp((((3333GGGG))))
  335.  
  336.  
  337.  
  338.        {
  339.            return(0);      /* report error */
  340.        }
  341.        status = regexec(&re, string, (size_t) 0, NULL, 0);
  342.        regfree(&re);
  343.        if (status != 0) {
  344.            return(0);      /* report error */
  345.        }
  346.        return(1);
  347.      }
  348.  
  349.      The following demonstrates how the REG_NOTBOL flag could be used with
  350.      _rrrr_eeee_gggg_eeee_xxxx_eeee_cccc()  to find all substrings in a line that match a pattern supplied
  351.      by a user. (For simplicity of the example, very little error checking is
  352.      done.)
  353.  
  354.      (void) regcomp (&re, pattern, 0);
  355.      /* this call to regexec( ) finds the first match
  356.       * on the line
  357.       */
  358.  
  359.      error = regexec (&re, &buffer[0], 1, &pm, 0);
  360.      while (error == 0) { /* while matches found */
  361.          /* substring found between pm.rm_so and pm.rm_eo */
  362.          /* This call to regexec( ) finds the next match */
  363.          error = regexec (&re, buffer + pm.rm_eo, 1,
  364.                           &pm, REG_NOTBOL);
  365.      }
  366.  
  367.  
  368. AAAAPPPPPPPPLLLLIIIICCCCAAAATTTTIIIIOOOONNNN UUUUSSSSAAAAGGGGEEEE
  369.      An application could use:
  370.           _rrrr_eeee_gggg_eeee_rrrr_rrrr_oooo_rrrr_((((_cccc_oooo_dddd_eeee_,,,,_pppp_rrrr_eeee_gggg_,,,,_((((_cccc_hhhh_aaaa_rrrr _****_))))_NNNN_UUUU_LLLL_LLLL_,,,,_((((_ssss_iiii_zzzz_eeee______tttt_))))_0000_))))
  371.      to find out how big a buffer is needed for the generated string, _mmmm_aaaa_llll_llll_oooo_cccc()
  372.      a buffer to hold the string, and then call _rrrr_eeee_gggg_eeee_rrrr_rrrr_oooo_rrrr()  again to get the
  373.      string. Alternately, it could allocate a fixed, static buffer that is big
  374.      enough to hold most strings, and then use _mmmm_aaaa_llll_llll_oooo_cccc()  to allocate a larger
  375.      buffer if it finds that this is too small.
  376.  
  377.  
  378. SSSSEEEEEEEE AAAALLLLSSSSOOOO
  379.      _ffff_nnnn_mmmm_aaaa_tttt_cccc_hhhh(3g), _gggg_llll_oooo_bbbb(3g), _<<<<_ssss_yyyy_ssss_////_tttt_yyyy_pppp_eeee_ssss_...._hhhh_>>>>, _<<<<_rrrr_eeee_gggg_eeee_xxxx_...._hhhh_>>>>
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.